package com.bw.flinkstreaming.state.job1;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
/**
 *  需求：当接收到的相同 key 的元素个数等于3个，计算这些元素的value的平均值。
 */
public class ValueStateWithCountAvgTest {
    public static void main(String[] args) throws  Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        DataStreamSource<Tuple2<Long, Long>> dataStreamSource =
                env.fromElements(Tuple2.of(5L, 12L), Tuple2.of(5L, 15L), Tuple2.of(6L, 9L), Tuple2.of(5L, 6L),Tuple2.of(6L, 3L), Tuple2.of(6L, 4L),Tuple2.of(5L, 6L));
        dataStreamSource
                .keyBy(0)
                .flatMap(new ValueStateWithCountAvg())
                .print();
        env.execute("ValueStateWithCountAvgTest");
    }
}